import { createSlice } from '@reduxjs/toolkit'

const userSlice = createSlice({
  name: 'user',
  initialState: {
    user: null,
  },
  reducers: {
    setUser: (state, action) => {
      state.user = action.payload
      if (action.payload === null) {
        localStorage.removeItem('user')
      } else {
        localStorage.setItem('user', JSON.stringify(action.payload))
      }
    },
    clearUser: state => {
      state.user = null
    },
  },
})

export const { setUser, clearUser } = userSlice.actions

export const selectUser = state => state.user.user

export default userSlice.reducer
